package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }


    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //去除前后空格
        if (queryString != null && queryString.length() > 0){
            queryString = queryString.trim();
        }

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        List<CheckItem> result = page.getResult();
        long total = page.getTotal();

        return new PageResult(total,result);
    }


    public void delete(Integer id){
        //注意：不能直接删除，需要判断当前检查项是否和检查组关联，如果已经和检查组进行了关联则不允许删除
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0){
            //存在引用,不能直接删除
            throw new RuntimeException("当前检查项被引用,不能直接删除");
        }else {
            checkItemDao.deleteById(id);
        }
    }


    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
